<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Campaign_join_model extends CI_Model {

    function get_candidate_number($campaign_id, $user_id) {

        $row = $this->db->select('candidate_number')->where('campaign_id', $campaign_id)->where('user_id', $user_id)->get('campaign_joins')->row_array();
        if ($row) {
            return $row['candidate_number'];
        }
        return FALSE;
    }

    function add_campaign_join($data_save) {
        $data_save['created_at'] = time();
        $data_save['published_at'] = time();
        $data_save['status'] = 'publishing';

        // Get Candidate Number //
        $data_save['candidate_number'] = 1;
        $query = $this->db->select_max('candidate_number')->where('campaign_id', $data_save['campaign_id'])->get('campaign_joins');
        if ($query->num_rows() > 0) {
            $data_save['candidate_number'] = $query->row()->candidate_number + 1;
        }

        $this->db->insert('campaign_joins', $data_save);


        // Add candidate count to campaigns //
        $this->db->set('candidate_amount', 'candidate_amount+1', FALSE)->where('id', $data_save['campaign_id'])->update('campaigns');

        return $data_save['candidate_number'];
    }

    function get_candidate($campaign_id, $candidate_number) {

        $candidate_number = floor($candidate_number);



        $this->db->select('campaign_joins.*,users.fb_id,users.firstname,users.lastname')->join('users', 'user_id=users.id')->where('campaign_id', $campaign_id)->where('status', 'publishing');

        if ($candidate_number == 0) {
            $this->db->order_by('RAND()');
        } else {
            $this->db->where('campaign_joins.candidate_number', $candidate_number);
        }
        return $this->db->get('campaign_joins')->row_array();
    }

    function get_all_candidate_list($campaign_id) {



        $query = $this->db->select('campaign_joins.id,candidate_number,title,users.fb_id,users.firstname,users.lastname')
                ->join('users', 'user_id=users.id')
                ->where('campaign_id', $campaign_id)
                ->where('campaign_joins.status', 'publishing')
                ->order_by('campaign_joins.candidate_number', 'DESC')
                ->get('campaign_joins');

        if ($query->num_rows() > 0) {
            return $query->result_array();
        }

        return array();
    }

    function get_other_candidate_list($campaign_id, $current_candidate_number) {

        $current_candidate_number = floor($current_candidate_number);

        $query = $this->db->select('campaign_joins.id,candidate_number,title,users.fb_id,users.firstname,users.lastname')
                ->join('users', 'user_id=users.id')
                ->where('campaign_id', $campaign_id)
                ->where('status', 'publishing')
                ->where('campaign_joins.candidate_number != ', $current_candidate_number)
                ->order_by('RAND()')
                ->limit(10)
                ->get('campaign_joins');

        if ($query->num_rows() > 0) {
            return $query->result_array();
        }

        return array();
    }

    function get_active_candidate() {
        
        
        $this->load->driver('cache', array('adapter' => 'file'));
        if (!$foo = $this->cache->get('get_active_candidate')) {
            // Find to day start //
            $start_date = strtotime(date('Y-m-d',time()));
            $foo = $this->db->select('
                campaign_joins.id as candidate_id,campaign_joins.title as candidate_title,campaign_joins.description,campaign_joins.register_content,campaign_joins.sum_positive_score,campaign_joins.sum_negative_score,campaign_joins.sum_wilson_score,campaign_joins.sum_view,candidate_number,campaign_joins.created_at,
                campaigns.id as campaign_id,campaigns.title as campaign_itle,candidate_vdo,candidate_image,candidate_text,
                users.fb_id,users.firstname,users.lastname,
                ')
                            ->join('campaign_activities', 'campaign_activities.campaign_join_id = campaign_joins.id AND campaign_activities.voted_at >= ' . $start_date)
                            ->join('campaigns', 'campaigns.id = campaign_joins.campaign_id AND campaigns.status="starting"')
                            ->join('users', 'users.id=campaign_joins.user_id')
                            ->where('campaign_joins.status', 'publishing')
                            ->order_by('(SELECT COUNT(*) FROM campaign_activities WHERE campaign_activities.campaign_join_id=candidate_id AND campaign_activities.voted_at >= ' . $start_date . ')', 'DESC')
                            ->limit(1)
                            ->get('campaign_joins')->row_array();
            $this->cache->save('get_active_candidate', $foo, 259200);
        }
        if ($foo) {
            return $foo;
        }
        return FALSE;
    }

    function get_noview_candidate_list($campaign_id, $user_id) {
        $query = $this->db->select('campaign_joins.id,candidate_number,title,users.fb_id,users.firstname,users.lastname')
                ->join('users', 'user_id=users.id')
                ->where('campaign_id', $campaign_id)
                ->where('status', 'publishing')
                ->where('campaign_joins.id NOT IN (SELECT campaign_join_id FROM campaign_activities WHERE user_id=' . $user_id . ')', null, FALSE)
                ->order_by('RAND()')
                ->limit(10)
                ->get('campaign_joins');

        if ($query->num_rows() > 0) {
            return $query->result_array();
        }

        return array();
    }

    function get_rank_candidate($campaign_id) {
        $query = $this->db->select('campaign_joins.id,candidate_number,title,users.fb_id,users.firstname,users.lastname,sum_positive_score,sum_view,sum_wilson_score')
                ->join('users', 'user_id=users.id')
                ->where('campaign_id', $campaign_id)
                ->where('status', 'publishing')
                ->order_by('campaign_joins.created_at', 'DESC')
                ->get('campaign_joins');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return array();
        }
    }

    function get_recent_voted_candidate($user_id) {

        $query = $this->db->select('campaigns.id as campaign_id,positive_vote,negative_vote,campaign_activities.id as candidate_id,candidate_number,campaign_joins.title as campaign_join_title,users.fb_id,users.firstname,users.lastname,campaigns.title as campaign_title')
                ->join('campaign_joins', 'campaign_activities.campaign_join_id = campaign_joins.id')
                ->join('campaigns', 'campaign_activities.campaign_id = campaigns.id')
                ->join('users', 'campaign_joins.user_id=users.id')
                ->where('campaign_activities.user_id', $user_id)
                ->where('(campaign_activities.positive_vote > 0 OR campaign_activities.negative_vote > 0)', null, FALSE)
                ->order_by('campaign_activities.voted_at', 'DESC')
                ->limit(10)
                ->get('campaign_activities');

        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return array();
        }
    }

}

